#include <iostream>

using namespace std;
const int N = 1000010;

int son[N][26], cnt[N], idx;
int n;
char s[N];

void Insert(char *str)
{
	int p = 0;
	for (int i = 0; str[i]; ++i)
	{
		int u = str[i] - 'a';
		if (!son[p][u]) //没有路先创造路
		{
			son[p][u] = ++idx;
		}
		p = son[p][u];
	}
	cnt[p]++; //标记此处有单词结尾;
}

int find(char *str)
{
	int p = 0;
	for (int i = 0; str[i]; ++i)
	{
		int u = str[i] - 'a';
		if (!son[p][u])
		{
			return 0;
		}
		p = son[p][u];
	}
	return cnt[p];
}

int main()
{
	cin >> n;
	while (n--)
	{
		char op[2];
		scanf("%s%s", op, s);
		if (op[0] == 'I')
		{
			Insert(s);
		}
		else
		{
			printf("%d\n", find(s));
		}
	}

	return 0;
}